/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package medicalstorebilling.ui.tab3medicine;

import database.dao.CategoriesDao;
import database.dao.MedicinesDao;
import database.entity.Categories;
import database.entity.Medicines;
import database.entity.StockDetails;
import java.util.ArrayList;
import java.util.List;
import javax.swing.GroupLayout;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 *
 * @author Administrator
 */
public class Phase2 extends javax.swing.JPanel {

    /**
     * Creates new form stock
     */
    private Medicines medicine = new Medicines();
    private MedicinesDao medicineDao = new MedicinesDao();
    private List<Categories> categories = new ArrayList<Categories>();
    private CategoriesDao categoriesDao = new CategoriesDao();
    private JPanel previous;
    private GroupLayout prevlayout;
    private Phase1Medicine phase1;
    private List<StockDetails> stockDetails = new ArrayList<StockDetails>();

    public List<StockDetails> getStockDetails() {
        return stockDetails;
    }

    public void setStockDetails(List<StockDetails> stockDetails) {
        List<StockDetails> old = getStockDetails();
        this.stockDetails = stockDetails;
        this.firePropertyChange("stockDetails", old, stockDetails);
    }

    /**
     * Get the value of categories
     *
     * @return the value of categories
     */
    public List<Categories> getCategories() {
        return categories;
    }

    /**
     * Set the value of categories
     *
     * @param categories new value of categories
     */
    public void setCategories(List<Categories> categories) {
        List<Categories> old = getCategories();
        this.categories = categories;
        this.firePropertyChange("categories", old, categories);
    }

    /**
     * Get the value of medicine
     *
     * @return the value of medicine
     */
    public Medicines getMedicine() {
        return medicine;
    }

    /**
     * Set the value of medicine
     *
     * @param medicine new value of medicine
     */
    public void setMedicine(Medicines medicine) {
        Medicines old = getMedicine();
        this.medicine = medicine;
        this.firePropertyChange("medicine", old, medicine);
    }

    public void updateIF() {
        if (this.medicine.getCode() == null) {
            txtTittle.setText("<html><b>Add new Medicine</b></html>");
            btnDeleteCancel.setText("Reset");
            btnEditSave.setText("Save");
            setCategories(categoriesDao.getAll());
            switchPanelEditStatus(true);
            txtNotStockAvail.setVisible(false);
            jPanel2.setVisible(false);
            return;
        } else {
            txtTittle.setText("<html><b>Edit Medicine</b></html>");
            btnDeleteCancel.setText("Delete");
            btnEditSave.setText("Edit");
            switchPanelEditStatus(false);
        }

        if (this.medicine.getStockDetails() == null || this.medicine.getStockDetails().isEmpty()) {
            txtNotStockAvail.setVisible(true);
            jPanel2.setVisible(false);
        } else if (!this.medicine.getStockDetails().isEmpty()) {
            txtNotStockAvail.setVisible(false);
            jPanel2.setVisible(true);
        }
    }

    public Phase2(Phase1Medicine phase1, Medicines medicine, JPanel previous, GroupLayout layout) {
        this.phase1 = phase1;
        this.previous = previous;
        if (medicine != null) {
            setMedicine(medicine);
            this.categories.add(medicine.getCategory());
        } else {
            Medicines med = new Medicines();
            setMedicine(med);
            setCategories(categoriesDao.getAll());
        }
        this.prevlayout = layout;
        initComponents();
    }

    private void switchPanelEditStatus(boolean flag) {
        txtMedName.setEditable(flag);
        txtMedPrice.setEditable(flag);
        txtMedDescription.setEditable(flag);
        txtCode.setEditable(flag);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        jPanel3 = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtMedDescription = new javax.swing.JTextArea();
        txtMedName = new javax.swing.JTextField();
        comboBoxCategory = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        txtMedPrice = new javax.swing.JTextField();
        btnEditSave = new javax.swing.JButton();
        btnDeleteCancel = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        txtCode = new javax.swing.JTextField();
        txtTittle = new javax.swing.JLabel();
        txtNotStockAvail = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();

        setPreferredSize(new java.awt.Dimension(800, 530));

        jPanel3.setPreferredSize(new java.awt.Dimension(800, 520));

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Medicine Details"));
        jPanel1.setPreferredSize(new java.awt.Dimension(780, 188));

        jLabel1.setText("Name");

        jLabel2.setText("Category");

        jLabel3.setText("Description");

        txtMedDescription.setColumns(20);
        txtMedDescription.setLineWrap(true);
        txtMedDescription.setRows(5);

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${medicine.description}"), txtMedDescription, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        txtMedDescription.setEditable(false);
        jScrollPane1.setViewportView(txtMedDescription);

        txtMedName.setPreferredSize(new java.awt.Dimension(200, 20));

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${medicine.name}"), txtMedName, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        txtMedName.setEditable(false);

        comboBoxCategory.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        comboBoxCategory.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        comboBoxCategory.setPreferredSize(new java.awt.Dimension(200, 20));

        org.jdesktop.beansbinding.ELProperty eLProperty = org.jdesktop.beansbinding.ELProperty.create("${categories}");
        org.jdesktop.swingbinding.JComboBoxBinding jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, eLProperty, comboBoxCategory);
        bindingGroup.addBinding(jComboBoxBinding);

        comboBoxCategory.setEditable(false);

        jLabel4.setText("Price ($)");

        txtMedPrice.setPreferredSize(new java.awt.Dimension(200, 20));

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${medicine.price}"), txtMedPrice, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        txtMedPrice.setEditable(false);

        btnEditSave.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/Edit.png"))); // NOI18N
        btnEditSave.setText("Edit");
        btnEditSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditSaveActionPerformed(evt);
            }
        });

        btnDeleteCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/Delete.png"))); // NOI18N
        btnDeleteCancel.setText("Delete");
        btnDeleteCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteCancelActionPerformed(evt);
            }
        });

        jLabel5.setText("Code");

        txtCode.setPreferredSize(new java.awt.Dimension(200, 20));

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${medicine.code}"), txtCode, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        txtCode.setEditable(false);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(btnEditSave, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnDeleteCancel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addGap(8, 8, 8))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel5)
                                .addGap(23, 23, 23)))
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtMedPrice, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(txtCode, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(txtMedName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(0, 0, Short.MAX_VALUE)))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 92, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(comboBoxCategory, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 337, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(txtMedName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(comboBoxCategory, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(txtMedPrice, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.LEADING))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(txtCode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(7, 7, 7)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnEditSave)
                            .addComponent(btnDeleteCancel)))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(13, Short.MAX_VALUE))
        );

        txtTittle.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        txtTittle.setText("<html><b>Add New Medicine</b></html>");

        txtNotStockAvail.setText("<html><b> No Stock Avaiable !!! </b></html>");

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Stocks Details"));

        jScrollPane2.setPreferredSize(new java.awt.Dimension(760, 230));

        eLProperty = org.jdesktop.beansbinding.ELProperty.create("${stockDetails}");
        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, eLProperty, jTable1);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${medicine.name}"));
        columnBinding.setColumnName("Medicine");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${purchase.name}"));
        columnBinding.setColumnName("Purchase");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${stockQuantity}"));
        columnBinding.setColumnName("Quantity");
        columnBinding.setColumnClass(Integer.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${expiredDate}"));
        columnBinding.setColumnName("Expired Date");
        columnBinding.setColumnClass(java.util.Date.class);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        jScrollPane2.setViewportView(jTable1);
        jTable1.getColumnModel().getColumn(0).setResizable(false);
        jTable1.getColumnModel().getColumn(1).setResizable(false);
        jTable1.getColumnModel().getColumn(2).setResizable(false);
        jTable1.getColumnModel().getColumn(3).setResizable(false);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 768, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/back.png"))); // NOI18N
        jButton1.setText("Exit");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(175, 175, 175)
                        .addComponent(txtTittle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGap(320, 320, 320)
                        .addComponent(txtNotStockAvail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtTittle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(txtNotStockAvail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 10, Short.MAX_VALUE))
        );

        bindingGroup.bind();
    }// </editor-fold>//GEN-END:initComponents

    private void btnEditSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditSaveActionPerformed
        // TODO add your handling code here:
        if (btnEditSave.getText().equals("Edit")) {
            switchPanelEditStatus(true);
            btnEditSave.setText("Save");
            setCategories(categoriesDao.getAll());
            btnDeleteCancel.setText("Cancel");
        } else {

            String error = "";

            double price = -1;
            if (txtMedName.getText().isEmpty()) {
                error += "Medicine name cannot be null\n";
            }
            if (txtMedDescription.getText().isEmpty()) {
                error += "Medicine Description cannot be null\n";
            }
            try {
                price = Double.parseDouble(txtMedPrice.getText());
            } catch (NumberFormatException e) {
                error += "Price must be a number\n";
            }
            if (price <= 0) {
                error += "Price must be greater than 0\n";
            }
            String code = txtCode.getText().trim();
            if (code.isEmpty() || code.length() != 8) {
                error += "Medicine's Code cannot be empty and must have 8 characters\n";
            }
            Medicines dup = medicineDao.getByCode(code);
            if ((dup != null) && (dup.getId() != medicine.getId())) {
                error += "Medicine's Code is duplicated.\n";
            }
            if (!error.isEmpty()) {
                JOptionPane.showMessageDialog(null, error, "Error", JOptionPane.ERROR_MESSAGE);
                return;
            }
            medicine.setCategory((Categories) comboBoxCategory.getSelectedItem());
            medicine = medicineDao.update(medicine);
            if (txtTittle.getText().equals("<html><b>Add new Medicine</b></html>")) {
                JOptionPane.showMessageDialog(this, "New Medicine Created", "Success", JOptionPane.WARNING_MESSAGE);
            } else {
                JOptionPane.showMessageDialog(this, "Medicine Updated", "Success", JOptionPane.WARNING_MESSAGE);
            }
            switchPanelEditStatus(false);
            categories.clear();
            categories.add(medicine.getCategory());
            btnDeleteCancel.setText("Delete");
            btnEditSave.setText("Edit");
        }
    }//GEN-LAST:event_btnEditSaveActionPerformed

    private void btnDeleteCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteCancelActionPerformed
        // TODO add your handling code here:
        if (btnDeleteCancel.getText().equals("Cancel")) {
            btnEditSave.setText("Edit");
            categories.clear();
            setMedicine(medicineDao.getById(medicine.getId()));
            categories.add(medicine.getCategory());
            comboBoxCategory.setSelectedIndex(categories.indexOf(medicine.getCategory()));
            switchPanelEditStatus(false);
            btnDeleteCancel.setText("Delete");
        } else if (btnDeleteCancel.getText().equals("Delete")) {
            if (JOptionPane.showConfirmDialog(this, "Do you want to delete this medicine", "Confirm", JOptionPane.WARNING_MESSAGE) == 0) {
                medicine.setStatus(2);
                medicine = medicineDao.update(medicine);
                JOptionPane.showMessageDialog(this, "Delete Successfully, now return to medicine list", "Alert", JOptionPane.INFORMATION_MESSAGE);
                this.phase1.updateStatus(evt);
                this.prevlayout.replace(this, this.previous);
            }
        } else {
            txtCode.setText("");
            txtMedDescription.setText("");
            txtMedName.setText("");
            txtMedPrice.setText("");
            comboBoxCategory.setSelectedIndex(0);
        }
    }//GEN-LAST:event_btnDeleteCancelActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        this.phase1.updateStatus(evt);
        this.prevlayout.replace(this, previous);
    }//GEN-LAST:event_jButton1ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnDeleteCancel;
    private javax.swing.JButton btnEditSave;
    private javax.swing.JComboBox comboBoxCategory;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField txtCode;
    private javax.swing.JTextArea txtMedDescription;
    private javax.swing.JTextField txtMedName;
    private javax.swing.JTextField txtMedPrice;
    private javax.swing.JLabel txtNotStockAvail;
    private javax.swing.JLabel txtTittle;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
}
